package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.ErInfoService;
import com.lianwei.hospital.Service.Impl.ErInfoImpl;
import com.lianwei.hospital.module.entity.ErInfo;
import com.lianwei.hospital.module.vo.ErVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/er")
public class ErInfoController {
    @Autowired
    private ErInfoService erInfoService;

    @GetMapping("/selectById")
    public Result<ErInfo> Select(String i) throws Exception {
        System.out.println(i+"\n\n");
        ErInfo erInfo = erInfoService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,erInfo.toString());
    }

    @PutMapping
    public Result<List<ErInfo>> Insert(@RequestBody ErInfo erInfo) throws Exception {
        System.out.println(erInfo);
        if(erInfoService.insert(erInfo)){
            List<ErInfo> erInfoList = erInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,erInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @DeleteMapping
    public Result<List<ErInfo>> Delete(@RequestBody ErInfo erInfo) throws Exception {
        System.out.println(erInfo);
        if(erInfoService.deleteByPrimaryKey(String.valueOf(erInfo.getId()))){
            List<ErInfo> erInfoList = erInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,erInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping
    public Result<List<ErInfo>> Update(@RequestBody ErInfo erInfo) throws Exception {
        System.out.println(erInfo);
        if(erInfoService.updateByPrimaryKey(erInfo)){
            List<ErInfo> erInfoList = erInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,erInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<ErInfo>> list(@RequestBody ErVO query) throws Exception {
        PageUtil<ErInfo> pageUtil= erInfoService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }
}
